<!--
功能：数据列表解析页面
作者：李成
日期：2023年11月30
-->
<script setup lang="ts">
import { ref, onMounted, nextTick } from "vue";
import type { pageModel } from "./utils/types.ts";
import { getFormStructAndData, saveDataFormd } from "@/api/dataList";
import { message } from "@/utils/message";
import { getToken } from "@/utils/auth";
const generateForm = ref();
const editData = ref();
const jsonData = ref();
const isSuccess = ref(false);
const uploadField = ref([]);

const props = withDefaults(defineProps<pageModel>(), {
  modeID: 0,
  rsID: 1,
  isSearch: false,
  tableName: "",
  isSuccess: false,
  isSheng: false,
  editCode: 0,
  paretCode: ""
});

onMounted(async () => {
  const newDiv = document.createElement("input");
  newDiv.id = "tos";
  newDiv.type = "text";
  newDiv.setAttribute("value", "Bearer " + getToken().accessToken);
  document.body.appendChild(newDiv);

  //获取表单结构和表单数据
  const Result = await getFormStructAndData({
    modeID: props.modeID,
    rsID: props.rsID
  });
  isSuccess.value = props.isSuccess;
  console.log("prosps" + JSON.stringify(props));
  jsonData.value = JSON.parse(Result.result.formStruct);
  editData.value = JSON.parse(Result.result.formData);
  setImageFileToken(Result.result.formStruct);
  setUploadFileToken(Result.result.formStruct);
  nextTick(() => {
    generateForm.value.refresh();
    if (Result.result.hideFields != null) {
      generateForm.value.hide(Result.result.hideFields);
    }
    if (Result.result.enabledFields != null) {
      generateForm.value.disabled(Result.result.enabledFields, true);
    }

    //要对值列表是否为空进行判断
    if (editData.value[0] != null) {
      generateForm.value.setData(editData.value[0]);
    }
    //要进行判断是否是父子表数据关系。
    if (props.paretCode != "" && editData.value[0] == null) {
      generateForm.value.setData(JSON.parse(props.paretCode));
    }

    //获取Token
    uploadField.value.forEach(item => {
      generateForm.value.setOptions(item, {
        headers: [
          { key: "Authorization", value: "Bearer " + getToken().accessToken }
        ]
      });
      //console.log(item, getToken().accessToken);
    });
  });
});

//imgupload
const setImageFileToken = (str: string) => {
  const regex = /name":".*?"/;
  let start = 0;
  while (start != -1) {
    start = str.indexOf("imgupload");
    if (start == -1) {
      break;
    }
    str = str.substring(start + 10);
    const oneFields = regex.exec(str);
    uploadField.value.push(oneFields[0].substring(7, oneFields[0].length - 1));
  }
};

//fileupload
const setUploadFileToken = (str: string) => {
  const regex = /name":".*?"/;
  let start = 0;
  while (start != -1) {
    start = str.indexOf("fileupload");
    if (start == -1) {
      break;
    }
    str = str.substring(start + 10);
    const oneFields = regex.exec(str);
    uploadField.value.push(oneFields[0].substring(7, oneFields[0].length - 1));
  }
};
const saveJson = async () => {
  const myfalg = true;
  generateForm.value.validate().then(async () => {
    generateForm.value.getData().then(async data => {
      const Result = await saveDataFormd({
        rsID: props.rsID,
        jsonStr: JSON.stringify(data),
        tableName: props.tableName,
        isSheng: props.isSheng,
        editCode: props.editCode
      });

      if (Result.isSuccess) {
        message("保存成功！", { type: "success" });
        //emit("delModel", true);
        isSuccess.value = true;
        //return false;
      } else {
        message(Result.msg, { type: "error" });
        isSuccess.value = false;
      }
    });
  });
  return myfalg;
};

defineExpose({ saveJson, isSuccess });
</script>

<template>
  <fm-generate-form
    :data="jsonData"
    data-token="123"
    ref="generateForm"
    :edit="!props.isSearch"
  />
</template>
